package com.group5.onlinegame.handler;


import com.group5.onlinegame.entity.Vip;

import com.group5.onlinegame.service.VipService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/")
public class VipHandler {
    @Autowired
    private VipService vipService;

    /*查*/
    @RequestMapping("viplist")
    @RequiresRoles("recharge")
    public ModelAndView getVipList(){
        ModelAndView mv=new ModelAndView("vip");
        List<Vip> list=vipService.getVipList();
        mv.addObject("list",list);
        return mv;
    }
    /*增*/
    @RequestMapping("addvip")
    @RequiresRoles("recharge")
    public String addVip(Vip vip){
        System.out.println(vip.getId());
        if (vip.getId()==null){
            vipService.addVip(vip);
        }else {
            vipService.upVip(vip);
        }

        return "redirect:/viplist";
    }
    /*删*/
    @RequestMapping("delvip")
    @ResponseBody
    @RequiresRoles("recharge")
    public String delVip(@RequestParam(value ="listid[]") int[] listid){
        try{
            vipService.delVip(listid);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }

    /*查一*/
    @RequestMapping("vipbyid")
    @ResponseBody
    @RequiresRoles("recharge")
    public Vip getVipbyid(@RequestParam(value = "id") int id){

        return vipService.getVipbyid(id);

    }

}
